//获取应用实例
var app = getApp()
Page({
  data: {
    sortType: 2,
    shopGoods: [],
    showLoading: true,
    noMoretip: false,
    page: 0
  },
  onLoad: function (e) {
    var that = this;
    var menuid = e.menuid;
    if(e.title){
      var menuname = e.title;
      wx.setNavigationBarTitle({
        title: menuname
      });
    }
    that.setData({
      menuid: menuid
    })
  },
  onShow: function () {
    var that = this;
    var data = {};
    var page = that.data.page;
    // var sortType = that.data.sortType;
    data.map = 'applet_mall_group_list';
    // data.sortType = sortType;
    data.page = page;
    data.version = 1;
    data.category = that.data.menuid;
    // console.log(that.data.recomm)
    // if (that.data.recomm){
    //   data.recomm = 1;
    // }
    wx.request({
      url: app.globalData.requestUrl,
      data: data,
      success: function (res) {
        // console.log(page);
        if (res.data.ec == 200) {
          console.log(res.data.data);
          var allArr = [];
          var initArr = that.data.shopGoods;
          var curArr = res.data.data.goods;
          var lastPageLength = curArr.length;
          if (page > 0) {
            allArr = initArr.concat(curArr);
          } else {
            allArr = res.data.data.goods;
          }
          that.setData({
            shopGoods: allArr,
            category: res.data.data.category
          })
          if (lastPageLength < 10) {
            that.setData({
              noMoretip: true,
              showLoading: false
            });
          }
          console.log(that.data.shopGoods);
        } else {
          console.log(res.data)
          if (page <= 0) {
            that.setData({
              shopGoods: [],
              noMoretip: false,
              showLoading: false
            })
          } else {
            that.setData({
              noMoretip: true,
              showLoading: false
            });
          }
        }
      },
      complete: function () {
        wx.hideToast();
        wx.stopPullDownRefresh();
      }
    });
  },
  onHide: function () {
    wx.removeStorage({
      key: 'cuxiao',
      success: function (res) {
        console.log(res.data)
      }
    })
  },
  onPullDownRefresh: function () {
    this.setData({
      page: 0,
      noMoretip: false,
      showLoading: true
    });
    this.onShow();
    console.log("下拉刷新");
  },
  onReachBottom: function () {
    var that = this;
    console.log("到达页面底部")
    var isMore = that.data.noMoretip;
    var page = that.data.page;
    page++;
    that.setData({
      page: page
    });
    if (isMore) {
      console.log("已完成或正在加载");
    } else {
      that.onShow();
    }
  },
  goodDetail: function (e) {
    var goodId = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/subpages1/groupGoodDetail/groupGoodDetail?goodid=' + goodId
    })
  },
  changeShowType: function (e) {
    var type = e.target.dataset.type;
    this.setData({
      showType: type
    })
  },
  searchPage: function () {
    wx.navigateTo({
      url: '/subpages1/searchList/searchList'
    })
  },
  sortGood: function (e) {
    var type = e.target.dataset.sort;
    this.setData({
      sortType: type
    })
    this.onPullDownRefresh();
  }
})